package cn.tedu.orderservice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Configuration
public class RabbitMqConfig {
    //可以通过配置文件来配置
    //正常订单
    public static final String ORDER_QUEUE = "order.queue";
    public static final String ORDER_EXCHANGE = "order.exchange";
    public static final String ORDER_ROUTING_KEY = "order.routing.key";
    //死信订单
    public static final String ORDER_DEAD_LETTER_QUEUE  = "order.dead.letter.queue";
    public static final String ORDER_DEAD_LETTER_EXCHANGE = "order.dead.letter.exchange";
    public static final String ORDER_DEAD_LETTER_ROUTING_KEY = "order.dead.letter.routing.key";

    //SpringBoot自动配置了ConnectionFactory与RabbitTemplate
/*    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory factory = new CachingConnectionFactory("localhost");
        factory.setUsername("guest");
        factory.setPassword("guest");
        factory.setVirtualHost("/");
        return factory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }*/


    /**
     *  创建订单队列
     * @return
     */
    @Bean
    public Queue getQueue(){
        log.debug("rabbitmq-创建订单队列");
        Map<String, Object> args = new HashMap<String, Object>();
        //死信的exchange
        args.put("x-dead-letter-exchange", ORDER_DEAD_LETTER_EXCHANGE);
        //死信的routing-key
        args.put("x-dead-letter-routing-key", ORDER_DEAD_LETTER_ROUTING_KEY);
        //指定存活时间
        Integer ttl =  5*60 * 1000;
        args.put("x-message-ttl", ttl);
        //惰性队列,在消息很多的时候,存到磁盘里,避免消息积压,占内存
        args.put("x-queue-mode","lazy");
        return new Queue(ORDER_QUEUE,true,true,false,args);
    }
    /**
     * 创建订单Exchange 交换机
     * @return
     */
    @Bean
    public DirectExchange orderTopicExchange() {
        log.debug("rabbitmq-创建订单交换机");
        return new DirectExchange(ORDER_EXCHANGE,true,false);
    }

    /**
     * 把订单队列绑定到订单交换机上
     * @return
     */
    @Bean
    public Binding orderBinding() {
        log.debug("rabbitmq-绑定");
        return BindingBuilder
                .bind(getQueue())
                .to(orderTopicExchange())
                .with(ORDER_ROUTING_KEY);
    }

    /**
     * 定义死信Exchange
     * @return
     */
    @Bean
    public TopicExchange deadLetterExchange(){
        return new TopicExchange(ORDER_DEAD_LETTER_EXCHANGE,true,false);
    }

    /**
     * 定义死信Queue
     * @return
     */
    @Bean
    public Queue deadLetterQueue(){
        return new Queue(ORDER_DEAD_LETTER_QUEUE,true,false,false);
    }

    /**
     * 将死信队列和死信的交换机进行绑定
     * @return
     */
    @Bean
    public Binding deadLetterBinding(){
        return BindingBuilder
                .bind(deadLetterQueue())
                .to(deadLetterExchange())
                .with(ORDER_DEAD_LETTER_ROUTING_KEY);
    }
}
